package pl.kaczanowscy.tomek.ipdl.examples

import pl.kaczanowscy.tomek.ipdl.GameResult;
import pl.kaczanowscy.tomek.ipdl.RoundListener;
import pl.kaczanowscy.tomek.ipdl.RoundResult;
import pl.kaczanowscy.tomek.ipdl.Strategy
import pl.kaczanowscy.tomek.ipdl.ClassicTwoPlayersGame
import pl.kaczanowscy.tomek.ipdl.Game
import pl.kaczanowscy.tomek.ipdl.TwoPlayersGame
import pl.kaczanowscy.tomek.ipdl.strategy.ALLD
import pl.kaczanowscy.tomek.ipdl.strategy.TFT

/**
 * An example of two players game. Uses RoundListener to print detailed information about the game.
 * @author tomek
 *
 */
public class TwoPlayersGameRoundListenerExample extends AbstractExample {
	
	private Strategy sA, sB
	
	private Game game
	private RoundListener listener
	
	private String roundsResults
	
	public TwoPlayersGameRoundListenerExample(ExampleBuilder builder) {
		super(builder)
		builder.setTitle "Two players game with round listener"
		roundsResults = ""
		sA = new ALLD()
		sB = new TFT()
		game = new ClassicTwoPlayersGame(sA, sB, 20)
		listener = new RoundListener() {
			
			private int roundNb = 1;
			@Override
			public void roundEnded(RoundResult result) {
				roundsResults += roundNb + ", moves: \t" + result.getMove(sA) + "\t" + result.getMove(sB) + "\n"
				roundsResults += roundNb++ + ", payoffs \t" + result.getPayoff(sA) + "\t" + result.getPayoff(sB) + "\n"
			}
		};
		game.addRoundListener listener
	}
	
	public static void main(String[] args) {
		TwoPlayersGameRoundListenerExample example = new TwoPlayersGameRoundListenerExample()
		example.show()
	}
	
	public void show() {
		builder.add("gameInfo", game.toString())
		GameResult result = game.play()
		builder.add("roundsResults", roundsResults)
		builder.add("gameResult", result.toString())
	}
}
